package com.nav.backend.mapper;

import com.nav.backend.pojo.CollectionPath;
import com.nav.backend.pojo.RingSearch;
import com.nav.backend.pojo.TwoPointsSearch;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * NavigateMapper 接口用于定义对导航相关表的数据库操作
 *
 * @author Seamher
 */
@Mapper
public interface NavigateMapper {

    /**
     * 添加两点导航历史记录
     *
     * @param searchOrigin      搜索起点
     * @param searchOriginX     搜索起点的 X 坐标
     * @param searchOriginY     搜索起点的 Y 坐标
     * @param searchDestination 搜索终点
     * @param searchDestinationX 搜索终点的 X 坐标
     * @param searchDestinationY 搜索终点的 Y 坐标
     * @param account           用户账号
     */
    @Insert("insert into two_points_search (search_origin, search_origin_x, search_origin_y, search_destination, search_destination_x, search_destination_y, account) " + "values (#{searchOrigin}, #{searchOriginX},#{searchOriginY},#{searchDestination},#{searchDestinationX},#{searchDestinationY},#{account})")
    void addHistory(String searchOrigin, Double searchOriginX, Double searchOriginY, String searchDestination, Double searchDestinationX, Double searchDestinationY, String account);

    /**
     * 添加环形导航历史记录
     *
     * @param searchLocation  搜索位置
     * @param searchLocationX 搜索位置的 X 坐标
     * @param searchLocationY 搜索位置的 Y 坐标
     * @param targetDistance  目标距离
     * @param account         用户账号
     */
    @Insert("insert into ring_search (ring_search_location, ring_search_location_x, ring_search_location_y, target_distance, account) " + "values (#{searchLocation}, #{searchLocationX}, #{searchLocationY}, #{targetDistance}, #{account})")
    void addRingHistory(String searchLocation, Double searchLocationX, Double searchLocationY, Integer targetDistance, String account);

    @Select("select * from two_points_search where search_origin = #{searchOrigin} and search_destination = #{searchDestination}")
    TwoPointsSearch searchBySearchOrigin(String searchOrigin, String searchDestination);

    @Select("select * from ring_search where ring_search_location = #{searchLocation}")
    RingSearch searchBySearchLocation(String searchLocation);

    @Select("SELECT * FROM collection_path " +
            "WHERE " +
            "collection_path_origin_x BETWEEN #{searchOriginX} - #{range} AND #{searchOriginX} + #{range} " +
            "AND collection_path_origin_y BETWEEN #{searchOriginY} - #{range} AND #{searchOriginY} + #{range} " +
            "AND collection_path_destination_x BETWEEN #{searchDestinationX} - #{range} AND #{searchDestinationX} + #{range} " +
            "AND collection_path_destination_y BETWEEN #{searchDestinationY} - #{range} AND #{searchDestinationY} + #{range}")
    List<CollectionPath> mark(Double searchOriginX, Double searchOriginY, Double searchDestinationX, Double searchDestinationY, Double range);
}
